package com.mhy.mvc.controller;

import com.mhy.mvc.pojo.Dog;
import com.mhy.mvc.pojo.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class ReqResController {

    @RequestMapping(value = "/common",produces = {"text/html; charset=UTF-8","application/json"})
    public String testCommonParams(String name, Integer age/*,HttpServletResponse response*/){
        //response.setContentType("text/html; charset=UTF-8");
        System.out.println(name);
        System.out.println(age);
        return "name: " + name + ", age: " + age;
    }

    @RequestMapping(value = "/common2",produces = {"text/html; charset=UTF-8","application/json"})
    public String testCommonParams2(@RequestParam("name") String username, Integer age/*,HttpServletResponse response*/){
        //response.setContentType("text/html; charset=UTF-8");
        System.out.println(username);
        System.out.println(age);
        return "name: " + username + ", age: " + age;
    }

    @RequestMapping(value = "/common3",produces = {"text/html; charset=UTF-8","application/json"})
    public String testCommonParam3(User user){
        System.out.println(user);
        //response.setContentType("text/html; charset=UTF-8");
//        System.out.println(name);
//        System.out.println(age);
        return "ok";
    }

    @RequestMapping(value = "/json", produces = {"application/json"})
    public String testJson(String name, Integer age){
        return "{\"name\":\"" + name + "\",\"age\":" + age + "}";
    }

    @RequestMapping("/pojo")
    public String testPojo(User user){
        System.out.println(user);
        return "ok";
    }

    @RequestMapping("list")
    public String testList(@RequestBody List<String> users){
        for (String user : users){
            System.out.println(user);
        }
        return "ok";
    }

    @RequestMapping("/array")
    public String testArray(@RequestBody String[] users){
        for (String user : users){
            System.out.println(user);
        }
        return "ok";
    }

    @RequestMapping("/toJsonlist")
    @ResponseBody
    public List<User> testTolist(){
        List<User> users = new ArrayList<>();
        users.add(new User(1, "莫皓宇", "123", new Date(),new Dog("John",1)));
        users.add(new User(2, "哈哈哈", "132",new Date(),new Dog("John",2)));
        return users;
    }
}
